package cn.cvs.service;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.dao.sysRole.SysRoleMapper;
import cn.cvs.entity.SysRole;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class SysRoleServiceImpl implements SysRoleService{
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    public SysRoleServiceImpl() {
    }

    public List<SysRole> getRoleList() throws Exception {
        return this.sysRoleMapper.selectList();
    }

    public boolean add(SysRole sysRole) throws Exception {
        boolean flag = false;
        sysRole.setCreatedTime(new Date());
        if (this.sysRoleMapper.add(sysRole) > 0) {
            flag = true;
        }

        return flag;
    }

    public boolean deleteById(Integer id) throws Exception {
        boolean flag = false;
        if (this.sysUserMapper.selectCount((String)null, id) == 0 && this.sysRoleMapper.deleteById(id) > 0) {
            flag = true;
        }

        return flag;
    }

    public boolean update(SysRole sysRole) throws Exception {
        boolean flag = false;
        sysRole.setUpdatedTime(new Date());
        if (this.sysRoleMapper.update(sysRole) > 0) {
            flag = true;
        }

        return flag;
    }

    public SysRole getById(Integer id) throws Exception {
        return this.sysRoleMapper.selectById(id);
    }

    public int getCountByCode(String code) throws Exception {
        return this.sysRoleMapper.selectByCode(code);
    }
}
